package com.yan.crowd.controller;

import com.yan.crowd.constant.CrowdConstant;
import com.yan.crowd.entity.po.MemberPO;
import com.yan.crowd.service.MemberProviderService;
import com.yan.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberProviderController {

    @Autowired
    private MemberProviderService memberProviderService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<MemberPO> saveMember(@RequestBody MemberPO memberPO){
        try{
            memberProviderService.saveMember(memberPO);
            return ResultEntity.successWithoutData();
        } catch (Exception e){

            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_ALREAD_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/get/member/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){
        try{
            memberProviderService.getMemberPOByLoginAcctRemote(loginacct);
            return ResultEntity.successWithoutData();
        } catch (Exception e){
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }

    }

}
